//
//  Extension+Button.swift
//  IMKeyBoard
//
//  Created by 汪志浩 on 2022/10/21.
//

import Foundation
import UIKit
public extension UIButton {
    /**
     设置选中的图片
     解决按钮选中状态下,点击未抬起的时候出现 nomal 的状态
     **/
    func k_setSelectImg(image:UIImage?){
        ///解决按钮选中状态下,点击未抬起的时候出现 nomal 的状态
        self.setImage(image, for: UIControl.State(rawValue: 1 << 2 | 1 << 0) )
    }
}

public enum ButtonEdgeInsetsStyle:Int {
    case top = 0
    case left = 1
    case bottom = 2
    case right = 3
}

public extension UIButton {
    
    convenience init(imageName:String,bgImageName:String) {
        self.init()
        setBackgroundImage(UIImage(named:bgImageName), for: .normal)
        setBackgroundImage(UIImage(named:bgImageName + "_highlighted"), for: .highlighted)
        setImage(UIImage(named:imageName), for: .normal)
        setImage(UIImage(named:imageName + "_highlighted"), for: .highlighted)
        sizeToFit()
    }

    convenience init(bgColor:UIColor,fontSize:CGFloat,title:String) {
        self.init()

        backgroundColor = bgColor
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        setTitle(title, for: .normal)
    }
    
    static func creatButton(imageName:String?, title:String) -> UIButton {
        let button = UIButton()
        if ((imageName?.count) != nil) {
            button.setImage(UIImage(named: imageName!), for: UIControl.State.normal)
        }
        button.setTitle(title, for: UIControl.State.normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        button.setTitleColor(UIColor.darkGray, for: UIControl.State.normal)
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        return button
    }
    
    
    func layoutButtonWithEdgeInsetsStyle(style:ButtonEdgeInsetsStyle,space:CGFloat) {
        let imageWidth = imageView?.size.width ?? 0
        let imageHeight = imageView?.size.height ?? 0
        
        let labelWidth = titleLabel?.size.width ?? 0
        let labelHeight = titleLabel?.size.height ?? 0
        
        var imageEdgeInsets = UIEdgeInsets.zero
        var labelEdgeInsets = UIEdgeInsets.zero
        
        switch style {
        case .top: do {
            imageEdgeInsets = UIEdgeInsets(top: -labelHeight-space/2, left: 0, bottom: 0, right: -labelWidth)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth, bottom: -imageHeight-space/2, right: 0)
        }
        case .left: do {
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -space/2, bottom: 0, right: space/2)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: space/2.0, bottom: 0, right: -space/2)
        }
        case .bottom: do {
            imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight - space/2, right: -labelWidth)
            labelEdgeInsets = UIEdgeInsets(top: -imageHeight - space, left: -imageWidth, bottom: 0, right: 0)
        }
        case .right: do {
            imageEdgeInsets = UIEdgeInsets(top: 0, left: labelWidth + space/2, bottom: 0, right:-labelWidth - space/2)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth-space/2.0, bottom: 0, right:imageWidth + space/2)
        }
        }
        
        self.titleEdgeInsets = labelEdgeInsets
        self.imageEdgeInsets = imageEdgeInsets
    }
}
